package org.whuims.pois.action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.action.result.DataVo;
import org.whuims.pois.dao.StarDao;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by 声伟 on 2014/5/20 0020.
 */
public class StarAction extends DispatchAction {
    StarDao starDao = new StarDao();

    public ActionForward show(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request, HttpServletResponse response) {

        int page = getPageNum(request);
        long topicId = getTopic(request);
        String userName = (String) request.getSession().getAttribute("userName");
        if (topicId <= 0 || userName == null) {
            return mapping.findForward("login");
        }
        int size = 10;
        List<DataVo> list = starDao.list(page, size, userName, topicId);
        request.setAttribute("result", list);
        request.setAttribute("tag", "我的关注");
        return mapping.findForward("success");
    }

    private int getPageNum(HttpServletRequest request) {
        String pageStr = request.getParameter("page");
        int page = 1;
        try {
            page = Integer.parseInt(pageStr);
        } catch (NumberFormatException e) {
            page = 1;
        }
        return page;
    }

    private long getTopic(HttpServletRequest request) {
        Object topic = request.getSession().getAttribute("topicId");
        if (topic == null) {
            return -1;
        }
        return (Long) topic;
    }
}
